/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.report.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.modules.manual.domain.dto.RaceManualDto;
import com.fzhucc.modules.manual.domain.dto.RaceManualQueryCriteria;
import com.fzhucc.modules.report.domain.RaceReport;
import com.fzhucc.modules.report.domain.dto.RaceReportDto;
import com.fzhucc.modules.report.service.RaceReportService;
import com.fzhucc.modules.report.domain.dto.RaceReportQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2023-08-16
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "大赛媒体报道:管理")
@RequestMapping("/api/raceReport")
public class RaceReportController {

    private final RaceReportService raceReportService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('raceReport:list')")
    public void exportRaceReport(HttpServletResponse response, RaceReportQueryCriteria criteria) throws IOException {
        raceReportService.download(raceReportService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询report")
    @ApiOperation("查询report")
    @PreAuthorize("@el.check('raceReport:list')")
    public ResponseEntity<Object> queryRaceReport(RaceReportQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(raceReportService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增report")
    @ApiOperation("新增report")
    @PreAuthorize("@el.check('raceReport:add')")
    public ResponseEntity<Object> createRaceReport(@Validated @RequestBody RaceReport resources){
        return new ResponseEntity<>(raceReportService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改report")
    @ApiOperation("修改report")
    @PreAuthorize("@el.check('raceReport:edit')")
    public ResponseEntity<Object> updateRaceReport(@Validated @RequestBody RaceReport resources){
        raceReportService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除report")
    @ApiOperation("删除report")
    @PreAuthorize("@el.check('raceReport:del')")
    public ResponseEntity<Object> deleteRaceReport(@RequestBody Integer[] ids) {
        raceReportService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousGetMapping(value = "/show")
    @Log("小程序展示大赛媒体报道")
    @ApiOperation("小程序展示大赛媒体报道")
    public ResponseEntity<Object> show(RaceReportQueryCriteria criteria, Pageable pageable){
        criteria.setIsShow(1);
        return new ResponseEntity<>(raceReportService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个大赛媒体报道")
    @ApiOperation("查询单个大赛媒体报道")
    public RaceReportDto queryReportById(@PathVariable Integer id) {
        return raceReportService.findById(id);
    }

    @GetMapping(value = "/getYearType")
    @Log("获取大赛媒体报道年份分类")
    @ApiOperation("获取大赛媒体报道年份分类")
    @AnonymousAccess
    public ResponseEntity<Object> getYearType() {
        return new ResponseEntity<>(raceReportService.getYearType(),HttpStatus.OK);
    }
}